﻿using GalaSoft.MvvmLight;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Popups;
using Youtube_HD.Model;

namespace Youtube_HD.Services
{
    public class ChannelDetailViewModel : ViewModelBase, IChannelDetailViewModel
    {
        ObservableCollection<YoutubeVideo> youtubeVideos;
        public ObservableCollection<YoutubeVideo> YoutubeVideos
        {
            get
            {
                if (youtubeVideos == null)
                    youtubeVideos = new ObservableCollection<YoutubeVideo>();
                return youtubeVideos;
            }
            set
            {
                youtubeVideos = value;
                RaisePropertyChanged(() => YoutubeVideos);
            }
        }
        bool searchComplete = false;
        public bool SearchComplete
        {
            get
            {
                return searchComplete;
            }
            set
            {
                searchComplete = value;
                RaisePropertyChanged(() => SearchComplete);
            }
        }
        IHttpChannel httpChannel;
        public ChannelDetailViewModel(IHttpChannel httpChannel)
        {
            this.httpChannel = httpChannel;
        }
        public async Task<feed> GetListDetailChannel(string playListId, Dictionary<string, string> param)
        {
            try
            {
                SearchComplete = true;
                var dataParm = HttpChannel.GetQueryString(param).Remove(0, 1);
                string urlData = "https://gdata.youtube.com/feeds/api/users/" + playListId + "/uploads?" + dataParm;
                var getData = await httpChannel.GetDataYoutubeAsync(urlData);


                if (getData != null)
                {
                    var data = ModelCreator<feed>.s_CreateInstanceFromXml(getData);
                    if (data.entry.Count() > 0)
                    {
                        foreach (var u in data.entry)
                        {
                            var duration = u.group.duration.seconds;
                            string a = u.link[0].href.ToString().Remove(0, 32);
                            var youtubeList = new YoutubeVideo()
                            {
                                Description = u.group.description.Value,
                                PubDate = "Published: " + u.published.ToString("dd-mm-yyyy"),
                                Thumbnail = "https://i.ytimg.com/vi/" + u.group.videoid + "/sddefault.jpg",
                                Title = u.title,
                                Id = a.Substring(0, 11),
                                Author = "Author: " + u.author.name,
                                ViewCount = "View: " + (u.statistics != null ? u.statistics.viewCount : 0),
                                Duration = "Duration: " + TimeSpan.FromSeconds(duration == 0 ? 0 : duration).Duration().ToString(),
                                Like = u.rating1.numLikes,
                                Unlike = u.rating1.numDislikes
                            };
                            YoutubeVideos.Add(youtubeList);
                        }
                    }
                }
                else
                {
                    new MessageDialog("Channel not found").ShowAsync();
                }
            }
            catch
            {
                new MessageDialog("Channel not found").ShowAsync();
            }
            
            SearchComplete = false;
            return null;
        }
    }
}
